<template>
  <div class="register-container">
    <div class="register-wrapper">
      <div class="register-left">
        <div class="register-banner">
          <h1>加入我们</h1>
          <p>成为物业保修系统的一员</p>
          <div class="benefits">
            <div class="benefit-item">
              <el-icon><Shield /></el-icon>
              <span>安全可靠</span>
            </div>
            <div class="benefit-item">
              <el-icon><Clock /></el-icon>
              <span>快速响应</span>
            </div>
            <div class="benefit-item">
              <el-icon><Service /></el-icon>
              <span>专业服务</span>
            </div>
          </div>
        </div>
      </div>
      
      <div class="register-right">
        <div class="register-form-container">
          <div class="form-header">
            <h2>用户注册</h2>
            <p>请填写您的详细信息完成注册</p>
          </div>
          
          <el-form
            ref="registerFormRef"
            :model="registerForm"
            :rules="registerRules"
            class="register-form"
            label-width="100px"
          >
        <el-form-item label="用户名" prop="username">
          <el-input
            v-model="registerForm.username"
            placeholder="请输入用户名"
            size="large"
          />
        </el-form-item>
        
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="registerForm.password"
            type="password"
            placeholder="请输入密码"
            size="large"
            show-password
          />
        </el-form-item>
        
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input
            v-model="registerForm.confirmPassword"
            type="password"
            placeholder="请再次输入密码"
            size="large"
            show-password
          />
        </el-form-item>
        
        <el-form-item label="真实姓名" prop="realName">
          <el-input
            v-model="registerForm.realName"
            placeholder="请输入真实姓名"
            size="large"
          />
        </el-form-item>
        
        <el-form-item label="手机号" prop="phone">
          <el-input
            v-model="registerForm.phone"
            placeholder="请输入手机号"
            size="large"
          />
        </el-form-item>
        
        <el-form-item label="身份证号" prop="idCard">
          <el-input
            v-model="registerForm.idCard"
            placeholder="请输入身份证号"
            size="large"
          />
        </el-form-item>
        
        <el-form-item label="房屋地址" prop="houseAddress">
          <el-input
            v-model="registerForm.houseAddress"
            placeholder="请输入房屋地址"
            size="large"
          />
        </el-form-item>
        
        <el-form-item label="房屋面积" prop="houseArea">
          <el-input-number
            v-model="registerForm.houseArea"
            placeholder="请输入房屋面积"
            size="large"
            :min="0"
            :precision="2"
            style="width: 100%"
          />
        </el-form-item>
        
        <el-form-item label="房屋类型" prop="houseType">
          <el-select
            v-model="registerForm.houseType"
            placeholder="请选择房屋类型"
            size="large"
            style="width: 100%"
          >
            <el-option label="住宅" value="住宅" />
            <el-option label="商铺" value="商铺" />
            <el-option label="办公" value="办公" />
            <el-option label="其他" value="其他" />
          </el-select>
        </el-form-item>
        
        <el-form-item label="用户角色" prop="roleId">
          <el-select
            v-model="registerForm.roleId"
            placeholder="请选择用户角色"
            size="large"
            style="width: 100%"
          >
            <el-option 
              v-for="role in roles" 
              :key="role.id" 
              :label="role.roleName" 
              :value="role.id"
            >
              <span>{{ role.roleName }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ role.description }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        
            <el-form-item>
              <el-button
                type="primary"
                size="large"
                class="register-button"
                :loading="loading"
                @click="handleRegister"
              >
                注册
              </el-button>
            </el-form-item>
          </el-form>
          
          <div class="register-footer">
            <p>
              已有账号？
              <el-link type="primary" @click="goToLogin">立即登录</el-link>
            </p>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { register, getRoles } from '@/api/auth'

const router = useRouter()

const registerFormRef = ref()
const loading = ref(false)

const registerForm = reactive({
  username: '',
  password: '',
  confirmPassword: '',
  realName: '',
  phone: '',
  idCard: '',
  houseAddress: '',
  houseArea: null,
  houseType: '',
  roleId: null
})

// 角色列表
const roles = ref([])

const validateConfirmPassword = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请再次输入密码'))
  } else if (value !== registerForm.password) {
    callback(new Error('两次输入密码不一致'))
  } else {
    callback()
  }
}

const registerRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度在3到20个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度在6到20个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, validator: validateConfirmPassword, trigger: 'blur' }
  ],
  realName: [
    { required: true, message: '请输入真实姓名', trigger: 'blur' }
  ],
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
  ],
  idCard: [
    { required: true, message: '请输入身份证号', trigger: 'blur' },
    { pattern: /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/, message: '身份证号格式不正确', trigger: 'blur' }
  ],
  houseAddress: [
    { required: true, message: '请输入房屋地址', trigger: 'blur' }
  ],
  houseArea: [
    { required: true, message: '请输入房屋面积', trigger: 'blur' }
  ],
  houseType: [
    { required: true, message: '请选择房屋类型', trigger: 'change' }
  ],
  roleId: [
    { required: true, message: '请选择用户角色', trigger: 'change' }
  ]
}

const handleRegister = async () => {
  if (!registerFormRef.value) return
  
  await registerFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      try {
        const { confirmPassword, ...formData } = registerForm
        await register(formData)
        ElMessage.success('注册成功，等待管理员审核')
        router.push('/login')
      } catch (error) {
        ElMessage.error(error.message || '注册失败')
      } finally {
        loading.value = false
      }
    }
  })
}

const goToLogin = () => {
  router.push('/login')
}

// 加载角色列表
const loadRoles = async () => {
  try {
    const response = await getRoles()
    roles.value = response.data
  } catch (error) {
    console.error('加载角色列表失败:', error)
    ElMessage.error('加载角色列表失败')
  }
}

// 组件挂载时加载角色
onMounted(() => {
  loadRoles()
})
</script>

<style scoped>
.register-container {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  overflow: hidden;
  margin: 0;
  padding: 0;
  z-index: 1;
}

.register-wrapper {
  display: flex;
  width: 100%;
  height: 100%;
}

.register-left {
  flex: 0 0 60%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 60px 40px;
  position: relative;
  overflow: hidden;
}

.register-banner {
  color: white;
  text-align: center;
  max-width: 500px;
}

.register-banner h1 {
  font-size: 48px;
  font-weight: 700;
  margin-bottom: 20px;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.register-banner p {
  font-size: 20px;
  margin-bottom: 40px;
  opacity: 0.9;
}

.benefits {
  display: flex;
  justify-content: space-around;
  margin-top: 60px;
}

.benefit-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

.benefit-item .el-icon {
  font-size: 32px;
  color: rgba(255, 255, 255, 0.9);
}

.benefit-item span {
  font-size: 16px;
  color: rgba(255, 255, 255, 0.8);
}

.register-right {
  flex: 0 0 40%;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 40px 30px;
  background: white;
  overflow-y: auto;
  position: relative;
}

.register-form-container {
  width: 100%;
  max-width: 400px;
  padding-top: 20px;
}

.form-header {
  text-align: center;
  margin-bottom: 30px;
}

.form-header h2 {
  font-size: 32px;
  font-weight: 600;
  color: #303133;
  margin-bottom: 10px;
}

.form-header p {
  font-size: 16px;
  color: #909399;
}

.register-form {
  margin-bottom: 20px;
}

.register-form .el-form-item {
  margin-bottom: 16px;
}

.register-button {
  width: 100%;
  height: 48px;
  font-size: 16px;
  font-weight: 500;
  border-radius: 6px;
}

.register-footer {
  text-align: center;
}

.register-footer p {
  color: #909399;
  font-size: 14px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .register-wrapper {
    flex-direction: column;
  }
  
  .register-left {
    padding: 40px 20px;
  }
  
  .register-banner h1 {
    font-size: 32px;
  }
  
  .register-banner p {
    font-size: 16px;
  }
  
  .benefits {
    flex-direction: column;
    gap: 20px;
    margin-top: 40px;
  }
  
  .register-right {
    padding: 20px;
  }
  
  .register-form-container {
    max-width: 100%;
  }
}
</style>